﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Common;

namespace ImpExpData
{
    abstract public class ImportData
    {
        public string Name { get; set; }
        public string FileName { get; set; }
        public string TableName{ get; set;}
        protected DataRowCollection cols;

        protected abstract void InsertData(SqlCeResultSet rs, XmlReader xmlReader);
        protected abstract void UpdateData(SqlCeResultSet rs, XmlReader xmlReader);
        protected abstract void DeleteData(SqlCeResultSet rs, XmlReader xmlReader);


        public void LoadData()
        {
            try
            {
                using (SqlCeConnection connection = WorkDataBase.CreateConnection())
                {
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    using (SqlCeCommand command = new SqlCeCommand(TableName, connection))
                    {
                        command.CommandType = CommandType.TableDirect;
                        using (SqlCeResultSet rs = command.ExecuteResultSet(ResultSetOptions.Updatable))
                        {
                            XmlTextReader xmlReader = new XmlTextReader(FileName);
                            XmlReaderSettings set = new XmlReaderSettings();
                            cols = rs.GetSchemaTable().Rows;
                            int indDb = 0, indImp = 0;
                            bool xmlGo = true, rsGo = true;
                            while (true)
                            {
                                if (xmlGo)
                                {
                                    while (xmlReader.Read() && xmlReader.Name != TableName)
                                    {
                                    }
                                }

                                indImp = !xmlReader.EOF &&
                                         xmlReader.GetAttribute(cols[0]["ColumnName"].ToString()) != null
                                             ? int.Parse(xmlReader.GetAttribute(cols[0]["ColumnName"].ToString()))
                                             : int.MaxValue;
                                indDb = rsGo && rs.Read() ? rs.GetInt32(0) : int.MaxValue;

                                if (indDb == int.MaxValue && indImp == int.MaxValue)
                                    break;

                                if (indDb == indImp)
                                {
                                    UpdateData(rs, xmlReader);
                                    rsGo = true;
                                    xmlGo = true;
                                    continue;
                                }

                                if (indDb > indImp)
                                {
                                    InsertData(rs, xmlReader);
                                    rsGo = false;
                                    xmlGo = true;
                                    continue;
                                }

                                if (indDb < indImp)
                                {
                                    DeleteData(rs, xmlReader);
                                    rsGo = true;
                                    xmlGo = false;
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}

